package com.ruoyi.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.ruoyi.ams.controller.AmsAssetInfoController;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.utils.SecurityUtils;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.security.annotation.PreAuthorize;
import com.ruoyi.system.domain.SysConfig;
import com.ruoyi.system.domain.SysNotify;
import com.ruoyi.system.service.ISysConfigService;
import com.ruoyi.system.service.ISysNotifyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 参数配置 信息操作处理
 * 
 * @author ruoyi
 */
@RestController
@RequestMapping("/system/config")
public class SysConfigController extends com.ruoyi.common.core.web.controller.BaseController
{
    @Autowired
    private ISysConfigService configService;

    @Autowired
    ISysNotifyService notifyService;

//    @Autowired
//    private RemoteAssetInfoService remoteAssetInfoService;
    @Autowired
AmsAssetInfoController assetInfoController;
    /**
     * 获取参数配置列表
     */

    @GetMapping("/test")
    public String test1(){
        return "test";
    }

    @GetMapping("/test-remote-service")
    public com.ruoyi.common.core.web.page.TableDataInfo test()
    {
      //  return remoteAssetInfoService.getUserInfo(SecurityConstants.INNER);
        return assetInfoController.list(null);
    }

    @PreAuthorize
    //@PreAuthorize(hasPermi = "system:config:list")
    @GetMapping("/list")
    public com.ruoyi.common.core.web.page.TableDataInfo list(SysConfig config)
    {
        startPage();
        List<SysConfig> list = configService.selectConfigList(config);
        return getDataTable(list);
    }

    @Log(title = "参数管理", businessType = BusinessType.EXPORT)
    @PreAuthorize
    //@PreAuthorize(hasPermi = "system:config:export")
    @PostMapping(value="/export", produces = "application/octet-stream")
    public void export(HttpServletResponse response, SysConfig config) throws IOException
    {
        List<SysConfig> list = configService.selectConfigList(config);
        com.ruoyi.common.core.utils.poi.ExcelUtil<SysConfig> util = new com.ruoyi.common.core.utils.poi.ExcelUtil<SysConfig>(SysConfig.class);
        util.exportExcel(response, list, "参数数据");
    }

    /**
     * 根据参数编号获取详细信息
     */
    @GetMapping(value = "/{configId}")
    public com.ruoyi.common.core.web.domain.AjaxResult getInfo(@PathVariable Long configId)
    {
        return com.ruoyi.common.core.web.domain.AjaxResult.success(configService.selectConfigById(configId));
    }

    /**
     * 根据参数键名查询参数值
     */
    @GetMapping(value = "/configKey/{configKey}")
    public com.ruoyi.common.core.web.domain.AjaxResult getConfigKey(@PathVariable String configKey)
    {
        return com.ruoyi.common.core.web.domain.AjaxResult.success(configService.selectConfigByKey(configKey));
    }

    /**
     * 新增参数配置
     */
    @PreAuthorize
    //@PreAuthorize(hasPermi = "system:config:add")
    @Log(title = "参数管理", businessType = BusinessType.INSERT)
    @PostMapping
    public com.ruoyi.common.core.web.domain.AjaxResult add(@Validated @RequestBody SysConfig config)
    {
        if (UserConstants.NOT_UNIQUE.equals(configService.checkConfigKeyUnique(config)))
        {
            return com.ruoyi.common.core.web.domain.AjaxResult.error("新增参数'" + config.getConfigName() + "'失败，参数键名已存在");
        }
        config.setCreateBy(SecurityUtils.getUsername());
        return toAjax(configService.insertConfig(config));
    }

    /**
     * 修改参数配置
     */
    @PreAuthorize
    //@PreAuthorize(hasPermi = "system:config:edit")
    @Log(title = "参数管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public com.ruoyi.common.core.web.domain.AjaxResult edit(@Validated @RequestBody SysConfig config)
    {
        if (UserConstants.NOT_UNIQUE.equals(configService.checkConfigKeyUnique(config)))
        {
            return com.ruoyi.common.core.web.domain.AjaxResult.error("修改参数'" + config.getConfigName() + "'失败，参数键名已存在");
        }
        config.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(configService.updateConfig(config));
    }

    /**
     * 删除参数配置
     */
    @PreAuthorize
    //@PreAuthorize(hasPermi = "system:config:remove")
    @Log(title = "参数管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{configIds}")
    public com.ruoyi.common.core.web.domain.AjaxResult remove(@PathVariable Long[] configIds)
    {
        configService.deleteConfigByIds(configIds);
        return success();
    }

    /**
     * 刷新参数缓存
     */
    @PreAuthorize
    //@PreAuthorize(hasPermi = "system:config:remove")
    @Log(title = "参数管理", businessType = BusinessType.CLEAN)
    @DeleteMapping("/refreshCache")
    public com.ruoyi.common.core.web.domain.AjaxResult refreshCache()
    {
        configService.resetConfigCache();
        return com.ruoyi.common.core.web.domain.AjaxResult.success();
    }

    /**
     *  1,"短信"
     *  2,"邮件"
     *  3,"微信"
     * @return
     */
    @GetMapping("/getNotifyConfig")
    public com.ruoyi.common.core.web.domain.AjaxResult getNotifyConfig(){
        Long userId = SecurityUtils.getUserId();

        List<SysNotify> list = notifyService.list(new QueryWrapper<SysNotify>().eq(SysNotify.SYS_USER_ID, userId));

        if(list != null && list.size() >0){
            return com.ruoyi.common.core.web.domain.AjaxResult.success("success",list.get(0).getNotifyTypeIds());
        }

        return com.ruoyi.common.core.web.domain.AjaxResult.success("",null);
    }

    @GetMapping("/updateNotifyConfig/{notifyConfig}")
    public com.ruoyi.common.core.web.domain.AjaxResult updateNotifyConfig(@PathVariable("notifyConfig") String notifyConfigs){
        if(notifyConfigs == null){
            notifyConfigs = "";
        }

        Long userId = SecurityUtils.getUserId();

        List<SysNotify> list = notifyService.list(new QueryWrapper<SysNotify>().eq(SysNotify.SYS_USER_ID, userId));

        boolean res = false;

        if(list != null && list.size() > 0){
           res = notifyService.update(new UpdateWrapper<SysNotify>().set(SysNotify.NOTIFY_TYPE_IDS, notifyConfigs).eq(SysNotify.SYS_USER_ID, userId));
        }else{
            SysNotify sysNotify = new SysNotify();
            sysNotify.setStatus(1);
            sysNotify.setNotifyTypeIds(notifyConfigs);
            sysNotify.setSysUserId(userId);
            res = notifyService.save(sysNotify);
        }

        if(res){
            return com.ruoyi.common.core.web.domain.AjaxResult.success("操作成功");
        }
        return com.ruoyi.common.core.web.domain.AjaxResult.success("操作失败");
    }
}
